﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using System.Web.Security;
using System.Transactions;

namespace MakeItWeb.Framework
{
    [Serializable]
    public class Member : BusinessObject
    {
        private int? m_iMemberId;
        private string m_sUsernameTxt;
        private string m_sPasswordTxt;
        private string m_sFirstnameTxt;
        private string m_sLastnameTxt;
        private string m_sKoreanNm;
        private string m_sEmailaddrTxt;
        private string m_sHomeAddressTxt;
        private string m_sHomeAddress2Txt;
        private string m_sHomeCityTxt;
        private string m_sHomeStateCd = string.Empty;
        private string m_sHomeZipCd;
        private string m_sHomeCountryNm = string.Empty;
        private string m_sHomePhonenumTxt;
        private string m_sCellPhonenumTxt;
        private DateTime? m_dtBirthdate;
        private string m_sIconFilenameTxt = string.Empty;
        private System.DateTime? m_dtMemberSinceDttm;
        private System.DateTime? m_dtLastloginDttm;
        private System.Guid m_guidConfirmationGuid = Guid.Empty;
        private System.DateTime m_dtCreatedDttm;
        private System.Guid m_guidUserId = Guid.Empty;
        private bool m_bIsLockedOut = false;

        #region Public properties

        // tbl_member: member_id
        public virtual int? MemberId
        {
            get
            {
                return this.m_iMemberId;
            }
            set
            {
                if (!value.Equals(m_iMemberId))
                {
                    this.m_iMemberId = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: username_txt
        public virtual string UsernameTxt
        {
            get
            {
                return this.m_sUsernameTxt;
            }
            set
            {
                if (!value.Equals(m_sUsernameTxt))
                {
                    this.m_sUsernameTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: password_txt
        public virtual string PasswordTxt
        {
            get
            {
                return this.m_sPasswordTxt;
            }
            set
            {
                if (!value.Equals(m_sPasswordTxt))
                {
                    this.m_sPasswordTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: firstname_txt
        public virtual string FirstnameTxt
        {
            get
            {
                return this.m_sFirstnameTxt;
            }
            set
            {
                if (!value.Equals(m_sFirstnameTxt))
                {
                    this.m_sFirstnameTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: lastname_txt
        public virtual string LastnameTxt
        {
            get
            {
                return this.m_sLastnameTxt;
            }
            set
            {
                if (!value.Equals(m_sLastnameTxt))
                {
                    this.m_sLastnameTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: korean_nm
        public virtual string KoreanNm
        {
            get { return m_sKoreanNm; }
            set
            {
                if (!value.Equals(m_sKoreanNm))
                {
                    m_sKoreanNm = value;
                    SetDirty();
                }
            }
        }

        // tbl_member: emailaddr_txt
        public virtual string EmailaddrTxt
        {
            get
            {
                return this.m_sEmailaddrTxt;
            }
            set
            {
                if (!value.Equals(m_sEmailaddrTxt))
                {
                    this.m_sEmailaddrTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: address_txt
        public virtual string HomeAddressTxt
        {
            get
            {
                return this.m_sHomeAddressTxt;
            }
            set
            {
                if (!value.Equals(m_sHomeAddressTxt))
                {
                    this.m_sHomeAddressTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: address2_txt
        public virtual string HomeAddress2Txt
        {
            get
            {
                return this.m_sHomeAddress2Txt;
            }
            set
            {
                if (!value.Equals(m_sHomeAddress2Txt))
                {
                    this.m_sHomeAddress2Txt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: city_txt
        public virtual string HomeCityTxt
        {
            get
            {
                return this.m_sHomeCityTxt;
            }
            set
            {
                if (!value.Equals(m_sHomeCityTxt))
                {
                    this.m_sHomeCityTxt = value;
                    this.SetDirty();
                }
            }
        }

        public virtual string HomeStateCd
        {
            get { return m_sHomeStateCd; }
            set
            {
                if (!value.Equals(m_sHomeStateCd))
                {
                    m_sHomeStateCd = value;
                    SetDirty();
                }
            }
        }

        // tbl_member: zip_cd
        public virtual string HomeZipCd
        {
            get
            {
                return this.m_sHomeZipCd;
            }
            set
            {
                if (!value.Equals(m_sHomeZipCd))
                {
                    this.m_sHomeZipCd = value;
                    this.SetDirty();
                }
            }
        }

        public virtual string HomeCountryNm
        {
            get { return m_sHomeCountryNm; }
        }

        // tbl_member: phonenum_txt
        public virtual string HomePhonenumTxt
        {
            get
            {
                return this.m_sHomePhonenumTxt;
            }
            set
            {
                if (!value.Equals(m_sHomePhonenumTxt))
                {
                    this.m_sHomePhonenumTxt = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: cellphone_txt
        public virtual string CellPhonenumTxt
        {
            get
            {
                return this.m_sCellPhonenumTxt;
            }
            set
            {
                if (!value.Equals(m_sCellPhonenumTxt))
                {
                    this.m_sCellPhonenumTxt = value;
                    this.SetDirty();
                }
            }
        }

        public virtual DateTime? Birthdate
        {
            get { return m_dtBirthdate; }
            set
            {
                if (!value.Equals(m_dtBirthdate))
                {
                    m_dtBirthdate = value;
                    SetDirty();
                }
            }
        }

        public virtual string IconFilenameTxt
        {
            get { return m_sIconFilenameTxt; }
            set
            {
                if (!value.Equals(m_sIconFilenameTxt))
                {
                    m_sIconFilenameTxt = value;
                    SetDirty();
                }
            }
        }

        // tbl_member: member_since_dt
        public virtual System.DateTime? MemberSinceDttm
        {
            get
            {
                return this.m_dtMemberSinceDttm;
            }
            set
            {
                if (!value.Equals(m_dtMemberSinceDttm))
                {
                    this.m_dtMemberSinceDttm = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: last_login_dttm
        public virtual System.DateTime? LastloginDttm
        {
            get
            {
                return this.m_dtLastloginDttm;
            }
            set
            {
                if (!value.Equals(m_dtLastloginDttm))
                {
                    this.m_dtLastloginDttm = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: confirmation_guid
        public virtual System.Guid ConfirmationGuid
        {
            get
            {
                return this.m_guidConfirmationGuid;
            }
            set
            {
                if (!value.Equals(m_guidConfirmationGuid))
                {
                    this.m_guidConfirmationGuid = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: created_dttm
        public virtual System.DateTime CreatedDttm
        {
            get
            {
                return this.m_dtCreatedDttm;
            }
            set
            {
                if (!value.Equals(m_dtCreatedDttm))
                {
                    this.m_dtCreatedDttm = value;
                    this.SetDirty();
                }
            }
        }

        // tbl_member: UserId
        public virtual System.Guid UserId
        {
            get
            {
                return this.m_guidUserId;
            }
            set
            {
                if (!value.Equals(m_guidUserId))
                {
                    this.m_guidUserId = value;
                    this.SetDirty();
                }
            }
        }

        public virtual bool IsLockedOut
        {
            get { return m_bIsLockedOut; }
        }


        public void UndoLockout()
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pUserId"] = UserId;
            GenericCud.ExecuteCud("proc_cm_SaveMemberUndoLockout", args);

            m_bIsLockedOut = false;
        }

        #endregion Public properties

        public override void Load(System.Data.DataRow dr)
        {
            if (DBNull.Value.Equals(dr["member_id"]) == false)
            {
                m_iMemberId = ((int)(dr["member_id"]));
            }
            if (DBNull.Value.Equals(dr["username_txt"]) == false)
            {
                m_sUsernameTxt = ((string)(dr["username_txt"]));
            }
            if (DBNull.Value.Equals(dr["password_txt"]) == false)
            {
                m_sPasswordTxt = ((string)(dr["password_txt"]));
            }
            if (DBNull.Value.Equals(dr["firstname_txt"]) == false)
            {
                m_sFirstnameTxt = ((string)(dr["firstname_txt"]));
            }
            if (DBNull.Value.Equals(dr["lastname_txt"]) == false)
            {
                m_sLastnameTxt = ((string)(dr["lastname_txt"]));
            }
            if (!dr.IsNull("korean_nm"))
                m_sKoreanNm = (string)dr["korean_nm"];
            if (DBNull.Value.Equals(dr["emailaddr_txt"]) == false)
            {
                m_sEmailaddrTxt = ((string)(dr["emailaddr_txt"]));
            }
            if (DBNull.Value.Equals(dr["address_txt"]) == false)
            {
                m_sHomeAddressTxt = ((string)(dr["address_txt"]));
            }
            if (DBNull.Value.Equals(dr["address2_txt"]) == false)
            {
                m_sHomeAddress2Txt = ((string)(dr["address2_txt"]));
            }
            if (DBNull.Value.Equals(dr["city_txt"]) == false)
            {
                m_sHomeCityTxt = ((string)(dr["city_txt"]));
            }
            if (DBNull.Value.Equals(dr["state_cd"]) == false)
                m_sHomeStateCd = (string)dr["state_cd"];

            if (DBNull.Value.Equals(dr["zip_cd"]) == false)
            {
                m_sHomeZipCd = ((string)(dr["zip_cd"]));
            }
            if (DBNull.Value.Equals(dr["homephone_txt"]) == false)
            {
                m_sHomePhonenumTxt = ((string)(dr["homephone_txt"]));
            }
            if (DBNull.Value.Equals(dr["cellphone_txt"]) == false)
            {
                m_sCellPhonenumTxt = ((string)(dr["cellphone_txt"]));
            }
            if (!dr.IsNull("birthdate_dt"))
                m_dtBirthdate = (DateTime)dr["birthdate_dt"];
            if (!dr.IsNull("icon_filename_txt"))
                m_sIconFilenameTxt = (string)dr["icon_filename_txt"];
            if (DBNull.Value.Equals(dr["member_since_dt"]) == false)
            {
                m_dtMemberSinceDttm = ((System.DateTime)(dr["member_since_dt"]));
            }
            if (DBNull.Value.Equals(dr["last_login_dttm"]) == false)
            {
                m_dtLastloginDttm = ((System.DateTime)(dr["last_login_dttm"]));
            }
            if (DBNull.Value.Equals(dr["confirmation_guid"]) == false)
            {
                m_guidConfirmationGuid = ((System.Guid)(dr["confirmation_guid"]));
            }
            if (DBNull.Value.Equals(dr["created_dttm"]) == false)
            {
                m_dtCreatedDttm = ((System.DateTime)(dr["created_dttm"]));
            }
            if (DBNull.Value.Equals(dr["UserId"]) == false)
            {
                m_guidUserId = ((System.Guid)(dr["UserId"]));
            }
            if (dr.Table.Columns.Contains("IsLockedOut") && !DBNull.Value.Equals(dr["IsLockedOut"]))
                m_bIsLockedOut = (bool)dr["IsLockedOut"];

            if (DBNull.Value.Equals(dr["country_nm"]) == false)
                m_sHomeCountryNm = (string)dr["country_nm"];

            this.SetLoaded();
        }

        public override void Save()
        {
            if (Dirty)
            {
                StoredProcArgs args = new StoredProcArgs();
                if (m_iMemberId.HasValue)
                    args["pMemberId"] = m_iMemberId;
                if (!string.IsNullOrEmpty(m_sFirstnameTxt))
                    args["pFirstnameTxt"] = m_sFirstnameTxt;
                if (!string.IsNullOrEmpty(m_sLastnameTxt))
                    args["pLastnameTxt"] = m_sLastnameTxt;
                if (!string.IsNullOrEmpty(m_sKoreanNm))
                    args["pKoreanNm"] = m_sKoreanNm;
                if (!string.IsNullOrEmpty(m_sEmailaddrTxt))
                    args["pEmailaddrTxt"] = m_sEmailaddrTxt;
                if (!string.IsNullOrEmpty(m_sHomeAddressTxt))
                    args["pHomeAddressTxt"] = m_sHomeAddressTxt;
                if (!string.IsNullOrEmpty(m_sHomeAddress2Txt))
                    args["pHomeAddress2Txt"] = m_sHomeAddress2Txt;
                if (!string.IsNullOrEmpty(m_sHomeCityTxt))
                    args["pHomeCityTxt"] = m_sHomeCityTxt;
                if (!string.IsNullOrEmpty(m_sHomeStateCd))
                    args["pHomeStateCd"] = m_sHomeStateCd;
                if (!string.IsNullOrEmpty(m_sHomeZipCd))
                    args["pHomeZipCd"] = m_sHomeZipCd;
                if (!string.IsNullOrEmpty(m_sHomePhonenumTxt))
                    args["pHomePhonenumTxt"] = m_sHomePhonenumTxt;
                if (!string.IsNullOrEmpty(m_sCellPhonenumTxt))
                    args["pCellPhonenumTxt"] = m_sCellPhonenumTxt;
                if (m_dtBirthdate.HasValue)
                    args["pBirthdate"] = m_dtBirthdate.Value;
                if (!string.IsNullOrEmpty(m_sIconFilenameTxt))
                    args["pIconFilenameTxt"] = m_sIconFilenameTxt;
                if (m_dtMemberSinceDttm.HasValue)
                    args["pMemberSinceDttm"] = m_dtMemberSinceDttm.Value;
                if (m_dtLastloginDttm.HasValue)
                    args["pLastloginDttm"] = m_dtLastloginDttm;
                if (!m_guidConfirmationGuid.Equals(Guid.Empty))
                    args["pConfirmationGuid"] = m_guidConfirmationGuid;
                args["pUserId"] = m_guidUserId;

                m_iMemberId = GenericCud.ExecuteCud("proc_cm_SaveMember", args, "pOutMemberId");
                this.SetDirty(false);
            }
        }

        public static Member GetCurrentMemberProfile()
        {
            return GetMember(System.Web.HttpContext.Current.User.Identity.Name);
        }

        public static Member GetMember(string sUserName)
        {
            Member mem = new Member();
            StoredProcArgs args = new StoredProcArgs();
            args["pUserName"] = sUserName;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetMemberProfile", args))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    mem.Load(ds.Tables[0].Rows[0]);
            }
            return mem;
        }

        public static Member GetMember(int iMemberId)
        {
            Member mem = new Member();
            StoredProcArgs args = new StoredProcArgs();
            args["pMemberId"] = iMemberId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetMemberById", args))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    mem.Load(ds.Tables[0].Rows[0]);
            }
            return mem;
        }

        public static Member GetMemberByConfirmationGuid(Guid guidConfirmation)
        {
            Member mem = new Member();
            StoredProcArgs args = new StoredProcArgs();
            args["pConfirmationGuid"] = guidConfirmation;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetMemberByConfirmationGuid", args))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    mem.Load(ds.Tables[0].Rows[0]);
            }
            return mem;
        }

        public static DataSet ListMember()
        {
            return GenericQry.GetDataSet("proc_cm_ListMember", new StoredProcArgs());
        }

        public static Member GetMemberByEmail(string sEmailaddrTxt)
        {
            Member mem = new Member();
            StoredProcArgs args = new StoredProcArgs();
            args["pEmailaddrTxt"] = sEmailaddrTxt;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetMemberByEmail", args))
            {
                if (ds.Tables[0].Rows.Count > 0)
                    mem.Load(ds.Tables[0].Rows[0]);
            }
            return mem;
        }

        public static void UpdateUserName(Guid guidUserId, string sUserName)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pUserId"] = guidUserId;
            args["pUserName"] = sUserName;
            GenericCud.ExecuteCud("proc_cm_UpdateUserName", args);
        }

        public static void DeleteMember(Member member)
        {
            using (TransactionScope scope = new TransactionScope())
            {
                StoredProcArgs args = new StoredProcArgs();
                args["pMemberId"] = member.MemberId.Value;
                GenericCud.ExecuteCud("proc_adm_DeleteMember", args);

                if (!string.IsNullOrEmpty(member.UsernameTxt))
                    Membership.DeleteUser(member.UsernameTxt, true);

                scope.Complete();
            }
        }

        public static string GenerateRandomPassword(int iLength)
        {
            string sChoice = "ABCDEFG";
            Random random = new Random();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < iLength; i++)
                sb.Append(sChoice.Substring(random.Next(sChoice.Length), 1));

            return sb.ToString();
        }

        public static Member GetReferringMember(string sFirstNm, string sLastNm, string sEmail)
        {
            StoredProcArgs args = new StoredProcArgs();
            if (!string.IsNullOrEmpty(sFirstNm))
                args["pFirstNm"] = sFirstNm;
            if (!string.IsNullOrEmpty(sLastNm))
                args["pLastNm"] = sLastNm;
            if (!string.IsNullOrEmpty(sEmail))
                args["pEmail"] = sEmail;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_GetReferringMember", args))
            {
                if (ds.Tables[0].Rows.Count == 1)
                {
                    Member mem = new Member();
                    mem.Load(ds.Tables[0].Rows[0]);
                    return mem;
                }
            }
            return null;
        }
    }
}